import BackGround from './runtime/background'
import GameInfo   from './runtime/gameinfo'
import DataBus    from './databus'
import DItems      from './d_items'
import DActionItem from './d_a_item'
import DPropItem   from './d_p_item'

const Left_IMG_SRC = 'images/left.png'
const Refresh_IMG_SRC = 'images/refresh.png'
const Right_IMG_SRC = 'images/right.png'
const Score_IMG_SRC = 'images/score.png'

let ctx   = canvas.getContext('2d')
let databus = new DataBus()
let start_x = databus.start_x
let start_y = databus.start_y
let c_height = databus.c_height
let line_num = databus.line_num
let diamondSize = databus.diamondSize
let actionSize = databus.actionSize

let gameinfo
let left_action
let refresh_action
let right_action
let scoreinfo
/**
 * 游戏主函数
 */
export default class Main {
  constructor() {
    // 维护当前requestAnimationFrame的id
    this.aniId    = 0
    // 适配区间
    databus.resetFrame();
    //创建道具
    databus.prop_item = databus.pool.getItemByClass('DPropItem', DPropItem)
    // 创建操作按钮
    this.createActions()

    this.restart()

    canvas.addEventListener('touchstart', this.touchStart)
    canvas.addEventListener('touchmove', this.touchMove)//touchmove 一旦监听多次调用，太过频繁
    canvas.addEventListener('touchend', this.touchEnd)
  }
  // 创建操作按钮
  createActions(){

    left_action = new DActionItem(Left_IMG_SRC, 3, databus.action_y,3,3,1);
    refresh_action = new DActionItem(Refresh_IMG_SRC, line_num/2 - 1.5, databus.action_y, 3, 3,2);

    right_action = new DActionItem(Right_IMG_SRC, line_num - 6, databus.action_y, 3, 3,3);

    // scoreinfo = new DActionItem(Score_IMG_SRC, 1, databus.start_y, 6, 2, 6);
  }

  touchStart(e){
    let x = e.touches[0].clientX
    let y = e.touches[0].clientY

    left_action.touch(x, y);
    refresh_action.touch(x, y);
    right_action.touch(x, y);

    // 游戏结束监听下一步动作
    if (databus.gameOver) {
      gameinfo.touchMonitor(x, y);
    }
  }

  touchMove(e) {
    let x = e.changedTouches[0].clientX
    let y = e.changedTouches[0].clientY

    left_action.touchMove(x, y);
    refresh_action.touchMove(x, y);
    right_action.touchMove(x, y);
  }
  
  touchEnd(e) {
    let x = e.changedTouches[0].clientX
    let y = e.changedTouches[0].clientY

    left_action.touchEnd(x, y);
    refresh_action.touchEnd(x, y);
    right_action.touchEnd(x, y);
  }

  restart() {

    this.bg       = new BackGround(ctx)
    this.gameinfo = new GameInfo()
    gameinfo = this.gameinfo

    this.bindLoop     = this.loop.bind(this)
    this.hasEventBind = false

    // // 清除上一局的动画
    window.cancelAnimationFrame(this.aniId);

    this.aniId = window.requestAnimationFrame(
      this.bindLoop,
      canvas
    )
  }
  /**
   * canvas重绘函数
   * 每一帧重新绘制所有的需要展示的元素
   */
  render() {
    
    ctx.clearRect(0, 0, canvas.width, canvas.height)

    this.bg.render(ctx)

    left_action.drawToCanvas(ctx)
    refresh_action.drawToCanvas(ctx)
    right_action.drawToCanvas(ctx)

    // scoreinfo.drawToCanvas(ctx)
    // ctx.fillStyle = "#ffffff"
    // ctx.font = "20px Arial"
    // ctx.fillText(
    //   databus.score,
    //   scoreinfo.x * diamondSize,
    //   databus.start_y * diamondSize
    // )
    
    //挑战模式展示道具
    if (databus.mutiGameType && databus.prop_item.visible){

      databus.prop_item.drawToCanvas(ctx)
    }
    //更新道具倒计时
    if (databus.time_in) {
      let time_left = Math.floor((databus.time_end - Date.now()) / 1000)
      if (time_left > 0) {
        ctx.fillStyle = "#ffffff"
        ctx.font = "20px Arial"
        ctx.fillText(
          time_left,
          diamondSize,
          2 * diamondSize
        )
      } else {
        databus.gameOver = true
        databus.saveScore_max()
      }
    }

    if (databus.propState === 5) //5.蒙面
    {
      
    } else {
      databus.run_diamonds.forEach((ditems) => {
        ditems.drawToCanvas(ctx)
      })
    }

    databus.floor_diamonds.forEach((ditems) => {
      ditems.drawToCanvas(ctx)
    })

    // 游戏结束停止帧循环
    if ( databus.gameOver ) {
      this.gameinfo.renderGameOver(ctx, databus.score)
    }

  }
  // 游戏逻辑更新主函数
  update() {

    if (databus.gameOver || databus.pause)//游戏结束 || 暂停
      return;

    left_action.update()
    refresh_action.update()
    right_action.update()
    
    //挑战模式展示道具
    if(databus.mutiGameType){

      databus.prop_item.update()
      if (databus.propState === 0 &&
        databus.prop_item.visible === false &&
        databus.floor_diamonds.length > 0) {
        
        databus.prop_item.reInit()
      }
    }

    if (databus.run_diamonds.length > 0) {
      databus.run_diamonds.forEach((ditems) => {
        ditems.update()
      })
    } else {
      let diamond = new DItems();
      diamond.init();
      databus.run_diamonds.push(diamond);
    }

    this.bg.update()

  }

  // 实现游戏帧循环
  loop() {
    
    this.update()
    this.render()

    this.aniId = window.requestAnimationFrame(
      this.bindLoop,
      canvas
    )
  }
}
